// AI侦探应用核心实体类定义

// 案件类型
export enum CaseType {
  MURDER = 'MURDER',         // 谋杀案
  THEFT = 'THEFT',           // 盗窃案
  FRAUD = 'FRAUD',           // 诈骗案
  KIDNAPPING = 'KIDNAPPING', // 绑架案
  OTHER = 'OTHER'            // 其他案件
}

// 案件状态
export enum CaseStatus {
  OPEN = 'OPEN',           // 开放中
  INVESTIGATING = 'INVESTIGATING', // 调查中
  SOLVED = 'SOLVED',       // 已解决
  CLOSED = 'CLOSED'        // 已关闭
}

// 嫌疑人状态
export enum SuspectStatus {
  SUSPECTED = 'SUSPECTED', // 被怀疑
  DETAINED = 'DETAINED',   // 被拘留
  CLEARED = 'CLEARED',     // 已排除
  CONVICTED = 'CONVICTED', // 已定罪
  ACQUITTED = 'ACQUITTED'  // 无罪释放
}

// 线索类型
export enum ClueType {
  PHYSICAL = 'PHYSICAL',      // 物证
  TESTIMONY = 'TESTIMONY',    // 证词
  DOCUMENT = 'DOCUMENT',      // 文件
  DIGITAL = 'DIGITAL',        // 数字证据
  OTHER = 'OTHER'             // 其他线索
}

// 人员基类
class Person {
  id: string;              // 唯一标识符
  name: string;            // 姓名
  age: number;             // 年龄
  gender: string;          // 性别
  address?: string;        // 地址（可选）
  phoneNumber?: string;    // 电话号码（可选）
  description?: string;    // 描述（可选）
  photoUrl?: string;       // 照片URL（可选）
  createdAt: Date;         // 创建时间
  updatedAt: Date;         // 更新时间

  constructor(
    id: string,
    name: string,
    age: number,
    gender: string,
    address?: string,
    phoneNumber?: string,
    description?: string,
    photoUrl?: string
  ) {
    this.id = id;
    this.name = name;
    this.age = age;
    this.gender = gender;
    this.address = address;
    this.phoneNumber = phoneNumber;
    this.description = description;
    this.photoUrl = photoUrl;
    this.createdAt = new Date();
    this.updatedAt = new Date();
  }
}

// 案件实体类
export class Case {
  id: string;                // 案件ID
  title: string;             // 案件标题
  description: string;       // 案件描述
  caseType: CaseType;        // 案件类型
  status: CaseStatus;        // 案件状态
  location: string;          // 案发地点
  occurredTime: Date;        // 案发时间
  reportedTime: Date;        // 报案时间
  investigatorId: string;    // 调查员ID
  victimId?: string;         // 受害者ID（可选）
  suspectIds: string[];      // 嫌疑人ID列表
  clueIds: string[];         // 线索ID列表
  aiAnalysis?: string;       // AI分析结果（可选）
  notes?: string;            // 案件笔记（可选）
  createdAt: Date;           // 创建时间
  updatedAt: Date;           // 更新时间

  constructor(
    id: string,
    title: string,
    description: string,
    caseType: CaseType,
    status: CaseStatus,
    location: string,
    occurredTime: Date,
    reportedTime: Date,
    investigatorId: string,
    victimId?: string,
    suspectIds: string[] = [],
    clueIds: string[] = [],
    aiAnalysis?: string,
    notes?: string
  ) {
    this.id = id;
    this.title = title;
    this.description = description;
    this.caseType = caseType;
    this.status = status;
    this.location = location;
    this.occurredTime = occurredTime;
    this.reportedTime = reportedTime;
    this.investigatorId = investigatorId;
    this.victimId = victimId;
    this.suspectIds = suspectIds;
    this.clueIds = clueIds;
    this.aiAnalysis = aiAnalysis;
    this.notes = notes;
    this.createdAt = new Date();
    this.updatedAt = new Date();
  }
}

// 嫌疑人实体类
export class Suspect extends Person {
  caseId: string;            // 关联案件ID
  status: SuspectStatus;     // 嫌疑人状态
  motive?: string;           // 作案动机（可选）
  alibi?: string;            // 不在场证明（可选）
  relationToVictim?: string; // 与受害者关系（可选）
  confession?: string;       // 供词（可选）
  evidenceStrength: number;  // 证据强度（1-10）

  constructor(
    id: string,
    name: string,
    age: number,
    gender: string,
    caseId: string,
    status: SuspectStatus,
    evidenceStrength: number,
    address?: string,
    phoneNumber?: string,
    description?: string,
    photoUrl?: string,
    motive?: string,
    alibi?: string,
    relationToVictim?: string,
    confession?: string
  ) {
    super(id, name, age, gender, address, phoneNumber, description, photoUrl);
    this.caseId = caseId;
    this.status = status;
    this.motive = motive;
    this.alibi = alibi;
    this.relationToVictim = relationToVictim;
    this.confession = confession;
    this.evidenceStrength = evidenceStrength;
  }
}

// 受害者实体类
export class Victim extends Person {
  caseId: string;            // 关联案件ID
  injuries?: string;         // 受伤情况（可选）
  causeOfDeath?: string;     // 死因（可选，谋杀案相关）
  lastSeen?: string;         // 最后出现地点（可选）
  lastSeenTime?: Date;       // 最后出现时间（可选）

  constructor(
    id: string,
    name: string,
    age: number,
    gender: string,
    caseId: string,
    address?: string,
    phoneNumber?: string,
    description?: string,
    photoUrl?: string,
    injuries?: string,
    causeOfDeath?: string,
    lastSeen?: string,
    lastSeenTime?: Date
  ) {
    super(id, name, age, gender, address, phoneNumber, description, photoUrl);
    this.caseId = caseId;
    this.injuries = injuries;
    this.causeOfDeath = causeOfDeath;
    this.lastSeen = lastSeen;
    this.lastSeenTime = lastSeenTime;
  }
}

// 调查员实体类
export class Investigator extends Person {
  badgeNumber: string;       // 警徽号码
  rank: string;              // 职级
  department: string;        // 部门
  specialty?: string;        // 专长（可选）
  solvedCasesCount: number;  // 已解决案件数

  constructor(
    id: string,
    name: string,
    age: number,
    gender: string,
    badgeNumber: string,
    rank: string,
    department: string,
    address?: string,
    phoneNumber?: string,
    description?: string,
    photoUrl?: string,
    specialty?: string,
    solvedCasesCount: number = 0
  ) {
    super(id, name, age, gender, address, phoneNumber, description, photoUrl);
    this.badgeNumber = badgeNumber;
    this.rank = rank;
    this.department = department;
    this.specialty = specialty;
    this.solvedCasesCount = solvedCasesCount;
  }
}

// 线索实体类
export class Clue {
  id: string;                // 线索ID
  caseId: string;            // 关联案件ID
  name: string;              // 线索名称
  type: ClueType;            // 线索类型
  description: string;       // 线索描述
  discoveryLocation: string; // 发现地点
  discoveryTime: Date;       // 发现时间
  collectedBy: string;       // 收集人ID
  importance: number;        // 重要性（1-10）
  relatedSuspectIds: string[]; // 相关嫌疑人ID列表
  evidencePhotoUrls?: string[]; // 证据照片URL列表（可选）
  analysisResult?: string;   // 分析结果（可选）
  isKeyEvidence: boolean;    // 是否关键证据
  createdAt: Date;           // 创建时间
  updatedAt: Date;           // 更新时间

  constructor(
    id: string,
    caseId: string,
    name: string,
    type: ClueType,
    description: string,
    discoveryLocation: string,
    discoveryTime: Date,
    collectedBy: string,
    importance: number,
    relatedSuspectIds: string[] = [],
    evidencePhotoUrls?: string[],
    analysisResult?: string,
    isKeyEvidence: boolean = false
  ) {
    this.id = id;
    this.caseId = caseId;
    this.name = name;
    this.type = type;
    this.description = description;
    this.discoveryLocation = discoveryLocation;
    this.discoveryTime = discoveryTime;
    this.collectedBy = collectedBy;
    this.importance = importance;
    this.relatedSuspectIds = relatedSuspectIds;
    this.evidencePhotoUrls = evidencePhotoUrls;
    this.analysisResult = analysisResult;
    this.isKeyEvidence = isKeyEvidence;
    this.createdAt = new Date();
    this.updatedAt = new Date();
  }
}

// AI分析结果实体类
export class AIAnalysis {
  id: string;                // 分析ID
  caseId: string;            // 关联案件ID
  analysisTime: Date;        // 分析时间
  confidenceScore: number;   // 置信度分数（0-100）
  suspectRanking: Array<{suspectId: string, probability: number}>; // 嫌疑人排序及概率
  keyFindings: string[];     // 关键发现
  recommendedActions: string[]; // 推荐行动
  fullReport: string;        // 完整报告
  analysisType: string;      // 分析类型
  createdAt: Date;           // 创建时间

  constructor(
    id: string,
    caseId: string,
    confidenceScore: number,
    suspectRanking: Array<{suspectId: string, probability: number}>,
    keyFindings: string[],
    recommendedActions: string[],
    fullReport: string,
    analysisType: string
  ) {
    this.id = id;
    this.caseId = caseId;
    this.analysisTime = new Date();
    this.confidenceScore = confidenceScore;
    this.suspectRanking = suspectRanking;
    this.keyFindings = keyFindings;
    this.recommendedActions = recommendedActions;
    this.fullReport = fullReport;
    this.analysisType = analysisType;
    this.createdAt = new Date();
  }
}

// 数据模型工厂类 - 用于创建实体实例
export class ModelFactory {
  // 生成唯一ID
  private static generateId(): string {
    return Date.now().toString(36) + Math.random().toString(36).substr(2);
  }

  // 创建案件实例
  static createCase(
    title: string,
    description: string,
    caseType: CaseType,
    location: string,
    occurredTime: Date,
    investigatorId: string,
    victimId?: string
  ): Case {
    return new Case(
      this.generateId(),
      title,
      description,
      caseType,
      CaseStatus.OPEN,
      location,
      occurredTime,
      new Date(),
      investigatorId,
      victimId
    );
  }

  // 创建嫌疑人实例
  static createSuspect(
    name: string,
    age: number,
    gender: string,
    caseId: string,
    address?: string,
    motive?: string
  ): Suspect {
    return new Suspect(
      this.generateId(),
      name,
      age,
      gender,
      caseId,
      SuspectStatus.SUSPECTED,
      5, // 默认证据强度
      address,
      undefined,
      undefined,
      undefined,
      motive
    );
  }

  // 创建线索实例
  static createClue(
    caseId: string,
    name: string,
    type: ClueType,
    description: string,
    discoveryLocation: string,
    collectedBy: string,
    importance: number = 5
  ): Clue {
    return new Clue(
      this.generateId(),
      caseId,
      name,
      type,
      description,
      discoveryLocation,
      new Date(),
      collectedBy,
      importance
    );
  }

  // 创建AI分析实例
  static createAIAnalysis(
    caseId: string,
    confidenceScore: number,
    suspectRanking: Array<{suspectId: string, probability: number}>,
    keyFindings: string[],
    recommendedActions: string[],
    fullReport: string,
    analysisType: string = 'comprehensive'
  ): AIAnalysis {
    return new AIAnalysis(
      this.generateId(),
      caseId,
      confidenceScore,
      suspectRanking,
      keyFindings,
      recommendedActions,
      fullReport,
      analysisType
    );
  }
}